package cn.zyq.ykb.service.task.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.task.Task;
import cn.zyq.ykb.beans.task.vo.TaskPackageVo;
import cn.zyq.ykb.mapper.task.TaskMapper;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.enums.core.ResponseCodeEnum;
import cn.zyq.ykb.system.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.task.TaskPackageMapper;
import cn.zyq.ykb.beans.task.TaskPackage;
import cn.zyq.ykb.service.task.TaskPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 任务包Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-06-01
 */
@Service
public class TaskPackageServiceImpl  extends BaseService implements TaskPackageService {

    @Autowired
    private TaskPackageMapper taskPackageMapper;
    @Autowired
    private TaskMapper taskMapper;

    /**
     * 查询任务包
     *
     * @param taskPackageID 任务包主键
     * @return 任务包
     */
    @Override
    public TaskPackage selectTaskPackageByTaskPackageID(Integer taskPackageID) {
        TaskPackage taskPackage = taskPackageMapper.selectByPrimaryKey(taskPackageID);
        if (taskPackage.getDeleted() == 1) {
            return null;
        }
        Task taskParam = new Task();
        taskParam.setTaskPackageID(taskPackageID);
        taskParam.setDeleted(0);
        List<Task> select = this.taskMapper.select(taskParam);
        taskPackage.setTasks(select);
        return taskPackage;
    }

    /**
     * 查询任务包
     *
     * @param serviceID 业务ID
     * @return 任务包
     */
    @Override
    public TaskPackage selectTaskPackageByServiceID(String serviceID) {
        TaskPackage param = new TaskPackage();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.taskPackageMapper.selectOne(param);
    }

    /**
     * 查询任务包列表
     *
     * @param pageParam
     * @return 任务包
     */
    @Override
    public PageInfo<TaskPackage> selectTaskPackageList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<TaskPackage> list = this.taskPackageMapper.list(pageParam.getKv());
        for (TaskPackage taskPackage : list) {
            TaskPackageVo taskPackageVo = this.taskMapper.selectByPackageID(taskPackage.getTaskPackageID());
            taskPackage.setTaskPackageVo(taskPackageVo);
        }
        return new PageInfo<>(list);
    }

    /**
     * 查询任务包列表
     *
     * @param param
     * @return 任务包
     */
    @Override
    public List<TaskPackage> selectTaskPackageList(TaskPackage param) {
        param.setDeleted(0);
        return this.taskPackageMapper.select(param);
    }
    /**
     * 新增任务包
     *
     * @param param 任务包
     * @return 结果
     */
    @Override
    public int insertTaskPackage(TaskPackage param) {
        param.setState(0);
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.taskPackageMapper.insertSelective(param);
    }

    /**
     * 修改任务包
     *
     * @param param 任务包
     * @return 结果
     */
    @Override
    public int updateTaskPackage(TaskPackage param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.taskPackageMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除任务包
     *
     * @param taskPackageIDs 需要删除的任务包主键
     * @return 结果
     */
    @Override
    public int deleteTaskPackageByTaskPackageIDs(Integer[] taskPackageIDs) {
        int i = 0;
        for (Integer taskPackageID : taskPackageIDs) {
            i += this.deleteTaskPackageByTaskPackageID(taskPackageID);
        }
        return i;
    }

    /**
     * 删除任务包信息
     *
     * @param taskPackageID 任务包主键
     * @return 结果
     */
    @Override
    public int deleteTaskPackageByTaskPackageID(Integer taskPackageID) {
        TaskPackage taskPackage = this.selectTaskPackageByTaskPackageID(taskPackageID);
        if (taskPackage == null) {
            return 0;
        } else {
            TaskPackage storeInfo = new TaskPackage();
            storeInfo.setTaskPackageID(taskPackageID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.taskPackageMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public int upAndDown(Integer taskPackageID) {
        TaskPackage taskPackage = this.taskPackageMapper.selectByPrimaryKey(taskPackageID);
        if (BeanUtil.isEmpty(taskPackage)){
            throw new BusinessException(ResponseCodeEnum.ERROR,"该任务包不存在");
        }
        if (taskPackage.getState().equals(0)){
            taskPackage.setState(1);
        }else {
            taskPackage.setState(0);
        }
        int i = this.updateTaskPackage(taskPackage);
        if (i==0){
            return 0;
        }
        return 1;
    }

    @Override
    public List<TaskPackage> getList() {
        return this.taskPackageMapper.selectAll();
    }
}
